<template>
  <div class="view-fill">
    <div v-if="!modifyInfo.visible" class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        title="资产验收明细"
        :needOperateAffair="true"
        :operateAffairData="affairTableInfo"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button
            v-if="
              $permission('assetAcceptance:update') &&
              infoList.data.status === 1
            "
            @click="update"
            >修改</el-button
          >
          <el-button
            v-if="
              $permission('assetAcceptance:acceptance') &&
              infoList.data.status === 1
            "
            @click="acceptance"
            >验收</el-button
          >
          <PopoverPrint
            ref="popoverShow"
            v-model:popoverVisible="popoverVisible"
            :canPrint="canPrint"
            :templateArr="templateArr"
            @tempClick="tempClick"
            @print="print"
          ></PopoverPrint>
          <!-- <el-button
            v-if="
              $permission('assetAcceptance:printDetail') &&
              infoList.data.status !== 1
            "
            @click="print"
            >打印</el-button
          > -->
          <el-button
            v-if="
              $permission('assetAcceptance:exportDetail') &&
              infoList.data.status !== 1
            "
            @click="exportExcel"
            >导出excel</el-button
          >
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="infoList" />
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')"></Tooltip>
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <page-table
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :summary="true"
                :check-box="false"
                :tab-index="true"
                :summary-field-list="tableInfo.summaryFieldList"
                :field-list="tableInfo.fieldList"
                :handle="tableInfo.handle"
                :isDynamic="true"
              >
              </page-table>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <order-modify
      v-if="modifyInfo.visible"
      :id="modifyInfo.id"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
    />
    <acceptanceDetail
      v-if="acceptanceDetailsInfo.visible"
      v-model:show-dialog="acceptanceDetailsInfo.visible"
      :info="acceptanceDetailsInfo.info"
    />
  </div>
</template>

<script setup name="AssetAcceptanceOrderDetail">
import {
  acceptanceEexcelDetail,
  acceptancePrintDetail,
  getAcceptanceDetailsAll,
  setAcceptance,
  getAcceptanceById,
} from '@/api/asset'
import { getCustomizeSetting } from '@/api/customize'
import { orderOpAffairAll } from '@/api/affair'
import { fatchTemplateByCode } from '@/api/base'
import acceptanceDetail from './acceptanceDetail'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'
import { updatePrintCount } from '@/api/mapping'
const OrderModify = defineAsyncComponent(() => import('./modify'))
const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps(['visible', 'id'])

const infoList = ref({
  data: {},
  fieldList: [
    { label: '单号', value: 'orderNo', type: 'text' },
    { label: '状态', value: 'statusValue', type: 'text' },
    { label: '验收部门', value: 'deptName', type: 'text' },
    { label: '验收人', value: 'acceptanceUserName', type: 'text' },
    { label: '验收时间', value: 'acceptanceTime', type: 'text' },
    {
      label: '验收结论',
      value: 'acceptanceConclusion',
      type: 'popover',
    },
    { label: '备注', value: 'remark', type: 'popover' },
  ],
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    customizeUserVO: {},
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.id,
      },
    },
    queryParams: {
      withDetails: true,
    },
  },
})
const modifyInfo = ref({
  visible: false,
  title: '修改验收单',
  id: '',
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  summaryFieldList: [
    { value: 'qty', formatter: proxy.$fn.qtyFormatter },
    { value: 'sumAmount', formatter: proxy.$fn.TripartiteMethod },
  ],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '80',
    btList: [
      {
        label: '验收详情',
        event: showDetail,
        show: true,
      },
    ],
  },
})
const acceptanceDetailsInfo = ref({
  visible: false,
  info: {},
})
const affairTableInfo = ref({
  api: orderOpAffairAll,
  query: {
    orderType: 64,
    orderId: props.id,
    sort: 'operate_time desc',
  },
})
const canPrint = computed(() => {
  return (
    proxy.$permission('assetAcceptance:printDetail') &&
    infoList.value.data.status !== 1
  )
})
const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})

watch(
  () => props.visible,
  (value) => {
    showDialog.value = value
  },
  {
    immediate: true,
  }
)
watch(
  () => modifyInfo.value.visible,
  (value) => {
    if (!value) {
      getMain()
      getList()
    }
  }
)

setData()
function setData() {
  let obj = {
    orderNo: {
      label: '单号',
      value: 'orderNo',
      componentType: 'input',
      show: false,
      type: 'column',
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...obj,
  })
  getCustomize()
}
function showDetail(row) {
  acceptanceDetailsInfo.value.visible = true
  acceptanceDetailsInfo.value.info = row
}
function acceptance() {
  let ids = []
  ids.push(infoList.value.data.id)
  setAcceptance(ids).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
function returnBack() {
  emits('returnBack', false)
}
function getMain() {
  getAcceptanceById(props.id).then((resp) => {
    if (resp && resp.success) {
      infoList.value.data = resp.data
    }
  })
}
function getList() {
  getAcceptanceDetailsAll(filterInfo.value.query).then((resp) => {
    if (resp && resp.success) {
      tableInfo.value.data = resp.data
    }
  })
}

function update() {
  modifyInfo.value.id = infoList.value.data.id
  modifyInfo.value.title = '修改验收单'
  modifyInfo.value.visible = true
}
const templateArr = ref([])
const popoverVisible = ref(false)
//打印
function print() {
  selectByCodeAndDept({
    tempCode: 'assetAcceptanceDetail',
    deptId: infoList.value.data.deptId,
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            orderId: props.id,
            orderNo: infoList.value.data.orderNo,
          },
          callback: printCallback,
          printCount: true,
          data: {},
        })
      }
    }
  })
  /*   fatchTemplateByCode({
    resourceCode: 'assetAcceptance:printDetail',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      getPrintData(temp)
    }
  }) */
}
function printCallback() {
  try {
    // 打印成功增加打印次数
    updatePrintCount({
      orderNo: infoList.value.data.orderNo,
      orderId: props.id,
      tempCode: 'assetAcceptanceDetail',
      orderType: 64,
    })
  } catch (error) {}
}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      orderId: props.id,
      orderNo: infoList.value.data.orderNo,
    },
    callback: printCallback,
    printCount: true,
    data: {},
  })
  popoverVisible.value = false
}
function getPrintData(temp) {
  let params = {
    tableKey:
      'assetAcceptanceOrderDetail-com.slip.spd.entity.vo.asset.AssetAcceptanceOrderDetailVO',
    orderIds: [props.id],
  }
  acceptancePrintDetail(params).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}
//导出excel
function exportExcel() {
  acceptanceEexcelDetail(filterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function getCustomize() {
  let param = {
    tableKey:
      'assetAcceptanceOrderDetail-com.slip.spd.entity.vo.asset.AssetAcceptanceOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      let obj = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail,
        true
      )
      tableInfo.value.fieldList = obj.fieldList
      getMain()
      getList()
    }
  })
}
</script>

<style scoped></style>
